var HttpStatus = new Object();
HttpStatus.UNAUTHORIZED = 401;
HttpStatus.SUCCESS = 200;

var ojurl = "http://localhost/OnlineJudge";
var tokenName = "token";
var localName = "name";

function getUrlVar(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0; i<vars.length; ++i) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return null;
}
function isNum(str) {
    var t = String(str);
    var len = t.length;
    for (var i=0; i<len; ++i) {
        if (t[i] < '0' || t[i] > '9') {
            return false;
        }
    }
    return true;
}
function isBlank(arg) {
    if (arg == null || arg == "") {
        return true;
    }
    var len = arg.length;
    for (var i=0; i<len; ++i) {
        if (arg[i] != ' ') {
            return false;
        }
    }
    return true;
}
function base26SytemToDec(t) {  //26进制转10进制
    t = String(t).toUpperCase();
    var ret = Number(0);
    var len = t.length;
    for (var i=0; i<len; ++i) {
        ret = ret * 26 + parseInt(Number(t[i].charCodeAt()-'A'.charCodeAt()));
    }
    return ret;
}
function getName(name) {
    var t = localStorage.getItem(name);
    if (t != null) {
        return t.toString();
    }
    return null;
}
function setName(name, a) {
    localStorage.setItem(name, a);
}
function removeName(name) {
    localStorage.removeItem(name);
}
function errorHandle(obj) {
    //unathorized
    var errno = obj.errno;
    if (errno == HttpStatus.UNAUTHORIZED) {
        sessionStorage.setItem("preURL", window.location.href);
        localStorage.removeItem(tokenName);
        localStorage.removeItem("name");
        window.location.pathname = loginPath;
    }
    else {
        if (obj.warnObjId != null) {
            document.getElementById(obj.warnObjId).textContent = obj.message;
        }
    }
}

//add problem start
function problem_DIV2(docType, initContent) {
    if (docType == "md") {
        document.getElementById("docUpload").innerHTML = '';
        var md=initContent;
        testEditor = editormd("docUpload", {
                width: "100%",
                height: "50em",
                path : '../plugs/editormd/lib/',
                markdown : md,
                codeFold : true,
                saveHTMLToTextarea : true,
                searchReplace : true,
                taskList : true,
                tex : true,                   // 开启科学公式TeX语言支持，默认关闭
                imageUpload : true,
                imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
                imageUploadURL : "/OnlineJudge/common/picture",
                toolbarIcons: function() {
                    return [
                                "undo", "redo", "|", 
                                "bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", "|", 
                                "h1", "h2", "h3", "h4", "h5", "h6", "|", 
                                "list-ul", "list-ol", "hr", "|",
                                "link", "reference-link", "qiniu", "code", "preformatted-text", "code-block", "table", "datetime", "html-entities", "pagebreak", "|",
                                "goto-line", "watch", "preview", "clear", "search", "|",
                                "help", "info"
                            ]
                },
                toolbarIconsClass : {
                    qiniu : "fa-picture-o"
                },
                lang : {
                    toolbar : {
                        qiniu : "上传图片"
                    }
                },
                toolbarHandlers : {
                    qiniu : function(cm, icon, cursor, selection) {
                        this.imageDialogQiniu();
                    }
                },
                onload : function() {
                }
            });
    }
    else if(docType == "html" || docType == "pdf") {
        var mydiv = document.getElementById("docUpload");
        mydiv.innerHTML = '';
        mydiv.className = 'layui-row';
        mydiv.style = '';

        var form = document.createElement("form");
        form.className = "layui-form";
        form.id = "problemUpload";

        var mydiv2 = document.createElement("div");
        mydiv2.className = "layui-col-md6 layui-col-md-offset3 layui-form-item";
        form.appendChild(mydiv2);

        var label1 = document.createElement("label");
        label1.className = "layui-form-label";
        label1.for = "problemFile";
        label1.innerHTML = "上传 " + docType + "<br />";
        mydiv2.appendChild(label1);

        var input1 = document.createElement("input");
        input1.type = "file";
        input1.accept = "." + docType;
        input1.className = "layui-input";
        input1.id = "problemFile";
        input1.name = "problemFile";
        mydiv2.appendChild(input1);

        mydiv.appendChild(form);
    }
    else {
      document.getElementById("docUpload").innerHTML = '<h1 style="color: red">请先完成题目基本信息的提交</h1>'
    }
}

function problem_DIV3(isSpj) {
        var mydiv = document.getElementById("fileUpload");
        mydiv.innerHTML = '';
        var mydiv2 = document.createElement("div");
        mydiv2.className = "layui-col-md6 layui-col-md-offset3 layui-form-item";

        var form = document.createElement("form");
        form.className = "layui-form";
        form.id = "dataUpload";
        form.appendChild(mydiv2);

        var label1 = document.createElement("label");
        label1.className = "";
        label1.for = "judgeFile";
        label1.innerHTML = "上传ZIP文件. <br /> (请确保你的测试文件名称为1-n, 并且以.in和.out为结尾) <br />";
        mydiv2.appendChild(label1);

        var input1 = document.createElement("input");
        input1.type = "file";
        input1.className = "layui-input";
        input1.style = "margin-bottom: 3em";
        input1.id = "judgeFile";
        input1.name = "judgeFile";
        input1.accept = ".zip";
        mydiv2.appendChild(input1);

        var label2 = document.createElement("label");
        label2.className = "";
        label2.for = "judgeFiles";
        label2.innerHTML = "<h3>请保证评测数据组数为 <span style='color: red'>" + judgeFiles + "</span></h3>";
        mydiv2.appendChild(label2);
        
        mydiv.appendChild(form);
        if(isSpj == '1') {
            // document.getElementById("fileUpload").innerHTML = '<h1>暂不支持SPJ</h1>';
            var fileUpload = document.getElementById("fileUpload");
            fileUpload.innerHTML += '<textarea id="myTextarea"></textarea>';
            editor = CodeMirror.fromTextArea(document.getElementById("myTextarea"), {
                lineNumbers:true,
                mode: "text/x-c++src",
                theme: "default",
                indentUnit: 4,
                indentWithTabs: true,
                matchBrackets: true,
                autoCloseBrackets: true, 
                foldGutter: true,              // 允许在行号位置折叠
                gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
            });
            editor.setSize("100%", "30em");
            var content = "//input the spj source code\n";
            content += "#include <stdio.h>\n";
            content += "\n";
            content += "int main(int argc, char** argv) {\n";
            content += "    FILE* f_in = fopen(argv[1], \"r\"); //std data input\n";
            content += "    FILE* f_out = fopen(argv[2], \"r\"); //std data output\n" ;
            content += "    FILE* f_code = fopen(argv[3], \"r\"); //source code\n";
            content += "    FILE* u_out = fopen(argv[4], \"r\"); //user output\n\n";
            content += "    return 0;\n";
            content += "}\n";
            editor.setValue(content);
        }
}

//add problem end